﻿
using BaseAuthRole.AspNetCore;
using BaseAuthRole.AspNetCore.Client;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaseLog.AspNetCore.DI
{
    public static class BaseAuthServiceCollectionExtensions
    {
        public static void AddBaseAuth(this IServiceCollection services,Action<BaseAuthOption> option)
         {
            if (services == null)
            {
                throw new ArgumentNullException("services");
            }

            services.Configure(option);

            services.AddSingleton<IBaseAuthClient, BaseAuthClient>((IServiceProvider s) =>
            {
                BaseAuthOption option = s.GetRequiredService<IOptions<BaseAuthOption>>().Value;

                if (string.IsNullOrEmpty(option.AppId)||string.IsNullOrEmpty(option.AuthDomain)||string.IsNullOrEmpty(option.Secret) || string.IsNullOrEmpty(option.AuthCodeUrl)) 
                {
                    throw new ArgumentException("BaseAuthOption");               
                }
                return new BaseAuthClient(option); 
            });

           // var a = services.BuildServiceProvider().GetRequiredService<IOptions<BaseLogOption>>().Value;

         

        }
    }
}
